const { baseUrl, CODE } = require('../../config/api.js')
const app = getApp()
Page({
  data: {
    category: 0,
  },
  onLoad(options) {
    this.setData({
      category: options.category
    })
  },
  onSubmit(e) {
    if (this.flag) return
    const { content, status } = e.detail.value
    this.flag = true
    // console.log(app.globalData.userInfo)
    const { avatarUrl, id } = app.globalData.userInfo
    // 给用户loading状态 防止频繁点击
    wx.showLoading({
      title: '正在提交...',
      mask: true
    })
    wx.request({
      url: baseUrl + "/api/postInfo",
      data: {
        userId: id,
        imageUrl: avatarUrl,
        title: '',
        status: status, // 默认是0代表实习贴，1代表租房贴
        refined: 0, // 是否是精品贴，前期都为0，为1时可以置顶
        content: content
      },
      method: 'POST',
      success: (res) => {
        res = res.data
        // console.log(res)
        wx.hideLoading()
        if (res.code == CODE) {
          this.back('提交成功', 'success') 
        }else {
          this.back('提交失败', 'none') 
        }
      },
      fail: (err) => {
        console.log(err)
        this.back('提交失败', 'none') 
      }
    })
  },
  back(msg, icon) {
    const url = this.data.category == 0 ? '/pages/index/index' : '/pages/rent/rent'
    wx.showToast({
      title: msg,
      icon: icon,
      duration: 2000,
      mask: true,
      success: () => {
        setTimeout(() => {
          wx.reLaunch({
            url: url
          })
        }, 1000)
      }
    })
  }
})